package trader.instant;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;

import trader.util.Util;

public class Trader {
    //private String[] instruments = {"BAC","BAD","BAF","BA6","BPQ","CCV","CCX","CDF","CDG","CDM","CLP","CTV","CTX","CU3","CU4","CU8","CU9","CXP","C0S","C0T","C0U","C0V","C0X","C0Y","C03","C04","C07","C1G","C1H","C1J","C1K","C13","C14","C17","DCK","DCL","DCN","DD4","DQP","DQV","DQX","DRC","DRD","DRX","DVF","DVV","DY1","EBB","EBC","EBF","EFA","EFD","ELQ","FED","FEE","FEF","FKQ","FKR","FNJ","FNQ","F6R","F6X","F70","GCR","GCS","GJN","GJS","GKF","GKG","GKQ","GKR","GKS","GKT","GKU","GKV","GKX","GKY","GK5","GLA","GLB","GL0","GL1","GL2","GL3","GL4","GL5","GL6","GL7","GL8","GL9","GSM","GSX","GVV","GY2","G2H","G2J","G2K","G2L","G2M","G2N","G2P","G2Q","G2R","G2S","G2T","G2U","G2V","G2X","G2Y","G30","G31","G32","G33","G34","G5L","G55","G6H","G63","HJA","HJB","HJC","HJE","HJ4","HJ5","HJ6","HJ7","HJ8","HJ9","HKC","HKR","HK8","HL6","HL7","HNN","HTA","HTB","HTC","HTD","HTE","HT0","HT3","HT9","HVF","HVG","HVH","HVJ","H0U","H0V","H0X","H0Y","H10","H11","H12","H13","H14","H15","H16","H17","H4A","H5A","H5H","H5J","JCY","JD0","JJJ","JKU","JKV","JM7","JM8","JQP","JQQ","JQR","JXS","JXT","JXU","JXV","JXX","JXY","JY0","JY1","JY2","JY3","JY4","JY5","J0X","J1A","J1X","J3N","KBG","KBN","KBP","KBY","KC1","KC3","KC5","KD2","KEE","KFD","KFJ","KFN","KHK","KHL","KHV","KHX","KHY","KJX","KJ0","KKA","KKP","KKQ","KK0","KK8","KLY","KMG","KMJ","KMM","KMN","KMQ","KMR","KMT","KMV","KMX","KMY","KM0","KN0","KN1","KN2","KN3","KN5","KN7","KN8","KN9","KPG","KRB","KR3","KR7","KS6","KXT","KXU","KY8","KY9","K0E","K0F","K0G","K0H","K0J","K0K","K4A","K4B","K4C","K4D","K4E","K4F","K4G","K4H","K4J","K4K","K4L","K4M","K4P","K4Q","K4R","K4S","K43","K44","K45","K46","K47","K48","K49","K5A","K6V","K7D","K74","K75","K77","K78","LJS","LJT","LJV","LL4","LMQ","LMR","LMS","LMT","LMU","LMV","LMX","LMY","LN0","LN1","LPB","LPH","LPL","LP7","LQY","LRM","LR6","LUD","LUE","LUF","LUN","LXL","LXM","LXN","LXP","LXQ","LXR","LX2","LX6","LYA","LYB","LYH","LYK","LYM","LYN","LY4","LY6","LY7","L0A","L0B","L0C","L0D","L0E","L0F","L0G","L0H","L0J","L0K","L0L","L0M","L0N","L0P","L0Q","L0R","L0S","L0T","L0U","L0V","L0X","L0Y","L05","L06","L07","L08","L09","L10","L11","L12","L13","L2Y","L3P","L4B","L4N","L6Q","L6R","L6S","L6T","L7D","L7F","L7N","L71","L72","MA5","MA8","MCU","MCV","MDA","MDB","MFA","MFF","MFG","MFL","MFM","MFN","MFS","MFT","MFU","MF8","MF9","MHN","MJX","MJ3","MJ4","MKA","MKB","MKF","MKG","MKH","MKN","MKP","MK0","MK5","MK9","MLJ","MNQ","MS4","MT0","MUA","MUB","MUM","MUP","MUQ","MUU","MUV","MUX","MUY","MU7","M1K","M1L","M1M","M4G","M4H","M4L","M4V","M50","M6G","M6L","M6M","M6S","M6X","M6Y","M7L","M7M","M7N","M7P","M7U","M70","M71","M8J","M8K","M8L","M80","M81","M82","M88","NBH","NBM","NHT","NMK","NML","NMM","NMN","NMP","NMT","NMU","NMV","NNA","NNT","NNV","NNX","NNY","NN1","NN9","NPF","NPG","NP0","NP1","NP2","NP3","NP4","NP5","NP6","NQC","NQD","NRA","NRB","NRC","NRD","NRE","NRF","NRG","NR7","NUB","NUD","NUG","NUJ","NUK","NUL","NUM","NUN","NUP","NUR","NUU","NUV","NU6","NVB","NVC","NVE","NVF","NVK","NVP","NVS","NVU","NV3","NX2","NX3","N1J","N1T","N2A","N2C","N2M","N26","N4S","N4T","N5G","N5K","N5V","N5Y","N58","N6V","N63","N66","N7D","N7E","N7F","N7G","N7H","N7J","N7K","N7L","N7M","N7N","N7P","N7Q","N7R","N7S","N7T","N7U","N8E","N8F","N8G","N8H","N80","N86","N87","N88","N89","PF5","PGD","PGT","PH1","PH6","PMX","PPE","PPV","PQD","PQE","PSC","PSF","PUC","PVN","P3J","P3M","QAC","QAD","QAE","QAL","QAM","QAN","QA3","QBA","QBG","QBP","QB3","QB4","QCC","QCR","QCT","QCV","QEG","QEH","QFQ","QFV","QGT","QHR","QHT","QH2","QJE","QJP","QJQ","QKJ","QKL","QKT","QK3","QK6","QK7","QLM","QLS","QLU","QLV","QLX","QME","QMM","QNA","QNB","QND","QNE","QNF","QNG","QNX","QNY","QN9","QPA","QPB","QPC","QPD","QPE","QPF","QPG","QPJ","QPK","QPM","QPN","QPR","QPS","QP0","QP1","QP2","QP3","QP4","QP5","QP6","QP7","QP8","QP9","QSF","QS3","QTL","QTN","QTP","QTR","QTU","QTV","QT4","QT5","QUA","QUB","QUC","QUF","QUJ","QUK","QUQ","QUR","QUS","QUT","QU1","QU2","QU3","QU4","QU6","QU7","QU8","QU9","QVA","QVB","QVC","QV3","QV4","QV5","QV6","QV7","QXC","Q0G","Q1B","Q1C","Q1D","Q1E","Q1F","Q1G","Q1H","Q1J","Q14","Q15","Q2S","Q30","Q31","Q4A","Q4B","Q4C","Q4D","Q4E","Q4F","Q4G","Q4H","Q4J","Q4K","Q4L","Q4M","Q4N","Q4P","Q4Q","Q4R","Q4S","Q45","Q46","Q47","Q48","Q49","Q6A","Q6B","Q6C","Q6D","Q6E","Q67","Q68","Q69","Q8L","Q8P","Q8V","Q8X","Q8Y","Q9G","Q9H","Q9P","Q9Q","Q9R","Q93","Q94","Q95","REA","REB","RFD","RFV","RFX","RG0","RKM","RKV","RL0","RL1","RL2","RL3","RL4","RQH","RQJ","RQN","RQS","RQX","RSR","RSS","RSU","RTA","RTB","RTM","RT6","RT7","RUN","RUU","RVA","RVB","RVC","RVD","RVE","RVF","RVK","RV0","RV1","RV6","RV7","RXC","RXF","RXH","RXK","RXM","RXN","RXT","RYB","RYE","RYF","RYG","RYH","RYR","RYS","RYT","RYU","RYV","RY5","RY6","RY9","R00","R1V","R1X","R3D","R3E","R41","R42","R5X","R5Y","R57","R58","R6A","R69","R7N","R8C","SAA","SAB","SAJ","SAK","SAL","SAM","SAN","SAX","SAY","SBA","SBF","SBG","SBH","SBM","SBT","SB9","SCA","SCE","SCF","SCG","SCH","SCS","SCT","SCU","SDB","SDC","SDD","SDQ","SDR","SDS","SDT","SDU","SEF","SEG","SEH","SE3","SE4","SFR","SFS","SFT","SF0","SF1","SGC","SGD","SGE","SGF","SGG","SGH","SGN","SGS","SGT","SGU","SGV","SGX","SGY","SG3","SG9","SHA","SHB","SHC","SHD","SHE","SHF","SHG","SHH","SHL","SHM","SHR","SHU","SHV","SHX","SHY","SH0","SH1","SH3","SH4","SH5","SH6","SH7","SH8","SH9","SJ0","SJ1","SJ2","SJ3","SJ4","SJ5","SJ6","SJ8","SKJ","SKK","SKT","SKU","SLC","SLD","SL1","SL2","SMB","SMC","SMD","SME","SMM","SNR","SNS","SNT","SNU","SNV","SNX","SNY","SPB","SPC","SPH","SPS","SPX","SPY","SP0","SP1","SP4","SP7","SQ0","SQ1","SRC","SRD","SRE","SSE","SSM","SSN","SSP","SSQ","SSS","SST","SSU","SSV","SSX","SSY","STJ","STK","STL","STM","STN","STR","ST1","ST2","ST3","ST4","ST5","SU1","SU2","SU8","SVN","SVP","SVR","SVU","SV0","SV1","SV2","SV5","SV6","SV7","SV8","SV9","SX7","S02","S3V","S3X","S4A","S4D","S4G","S4H","S4S","S44","S5Q","S6K","S7F","S7G","S7J","S7L","S7M","S7N","S7S","S7T","S78","S79" };
    private String[] instruments = { "GL4" };
    
    public static void main(String[] args) {
        new Trader().run();
    }
    
    public void run() {
        if (!Util.login(Util.MAIL, Util.PASSWORD)) {
            System.out.println("Failed to login");
            return;
        }
        
        //System.out.println("Starting " + instruments.length / Util.degree + " pollers");
        
        //int i = new Random().nextInt(Util.degree);
        for (String id : instruments) {
            //if (id != null && i++ % Util.degree == 0) {
                new Thread(new Poller(id)).start();
            //}
        }
        
        while (true) {
            if (Util.cashChanged.getAndSet(false)) {
                Util.atomicCash.set((long)getPortfolioCash(Util.buyPortfolio));
                System.out.println("Updating portfolio cash: " + Util.atomicCash);
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
    }
    
    private double getPortfolioCash(int portfolio) {
        URL url = null;
        try {
            url = new URL(Util.BASE_URL + "/portfolio_overview.php?port_id=" + portfolio);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Cookie", Util.sessionId);
            String line = "";
            String magic = "<td class=\"arial_11\" id=\"available_funds\" style=\"background-color:#FFFFFF; text-align:right\">";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = in.readLine()) != null) {
                line = line.trim();
                if (line.startsWith(magic)) {
                    return Double.parseDouble(line.substring(magic.length(), line.length() - 5));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }
}
